package com.hou.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hou.commonutils.R;
import com.hou.eduservice.entity.EduCourse;
import com.hou.eduservice.entity.EduTeacher;
import com.hou.eduservice.entity.vo.CourseInfoVo;
import com.hou.eduservice.entity.vo.CoursePublicVo;
import com.hou.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-10-17
 */
@Api(description = "课程操作")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    // 添加课程
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) throws Exception {
        String id = courseService.saveInfo(courseInfoVo);
        return R.ok().data("id",id);
    }

    // 课程信息回显
    @GetMapping("getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId){
        CourseInfoVo courseInfo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo",courseInfo);
    }

    // 修改课程信息
    @PostMapping("updateCourse")
    public R updateCourse(@RequestBody CourseInfoVo courseInfoVo){

        courseService.updateCourse(courseInfoVo);

        return R.ok();
    }

    // 课程全部信息
    @GetMapping("getAll/{id}")
    public R getAll(@PathVariable String id){

        CoursePublicVo course = courseService.getAll(id);

        return R.ok().data("course",course);
    }

    // 课程最终发布
    @PostMapping("announce/{id}")
    public R announceCourse(@PathVariable String id){

        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");

        courseService.updateById(eduCourse);

        return R.ok();
    }




    // 分页显示所有课程
    @GetMapping("getAllCourse/{current}/{limit}")
    public R getAllCourse(@PathVariable Long current, @PathVariable Long limit){

        // 创建page对象
        Page<EduCourse> coursePage = new Page<>(current, limit);

        // 调用方法实现分页
        IPage<EduCourse> pageCourse = courseService.page(coursePage, null);

        long total = pageCourse.getTotal(); // 总记录数
        List<EduCourse> records = pageCourse.getRecords(); // 数据list集合

        Map map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);

        return R.ok().data(map);
    }

    //删除课程全部信息
    @DeleteMapping("deleteAllInfo")
    public R deleteAllInfo(String id) throws Exception {
        courseService.deleteAllInfo(id);
        return R.ok();
    }

}

